package cn.yh.register.user.service.impl;

import cn.yh.register.client.cmn.CmnFeignClient;
import cn.yh.register.model.user.Patient;
import cn.yh.register.user.mapper.PatientMapper;
import cn.yh.register.user.service.PatientService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient > implements PatientService {

    @Autowired
    private CmnFeignClient cmnFeignClient;

    /**
     * 获取就诊人列表
     *
     * @param userId 用户id
     * @return 就诊人列表
     */
    @Override
    public List<Patient> findByUserId(Long userId) {
        List<Patient> patients = baseMapper.selectList(new LambdaQueryWrapper<Patient>().eq(Patient::getUserId, userId));
        patients.forEach(this::packPatient);
        return patients;
    }


    /**
     * 根据id查询就诊人
     * @param patientId  就诊人id
     * @return 就诊人信息
     */
    @Override
    public Patient getPatient(Long patientId) {
        Patient patient = baseMapper.selectById(patientId);
        this.packPatient(patient);
        return patient;
    }

    //处理数据  根据编码获取名称
    private void packPatient(Patient patient){
        //就诊人证件类型
        String certificatesType = cmnFeignClient.getName(patient.getCertificatesType());
        //省
        String province = cmnFeignClient.getName(patient.getProvinceCode());
        //市
        String city = cmnFeignClient.getName(patient.getCityCode());
        //区
        String district = cmnFeignClient.getName(patient.getDistrictCode());
        //联系人证件类型
        if (!StringUtils.isEmpty(patient.getContactsCertificatesType())){
            String contactsCertificatesType = cmnFeignClient.getName(patient.getContactsCertificatesType());
            patient.getParam().put("contactsCertificatesTypeString",contactsCertificatesType);
        }
        //存入param属性
        patient.getParam().put("certificatesTypeString",certificatesType);
        patient.getParam().put("provinceString",province);
        patient.getParam().put("cityString",city);
        patient.getParam().put("districtString",district);
        patient.getParam().put("fullAddress",province + city + district + patient.getAddress());

    }
}
